
use "Study 1 US.dta"

gen ID_strength = (Q6_a + Q6_b + Q6_c)/30

gen ideology_cat3 = 1 if Q1<49
recode ideology_cat3 (.= 2) if Q1>51
replace ideology_cat = ideology_cat3

**Mapping Norms

*Calculate (Dis)approve
forval k = 1/22 {
gen Q2_`k'_diss = 1 if Q2_`k'<5
recode Q2_`k'_diss (.=0)

gen Q2_`k'_appr = 1 if Q2_`k'>5
recode Q2_`k'_appr (.=0)
}

*Calculate mass approve and disapprove

gen Left_diss = .
gen Left_diss_se = .
gen Left_appr = .
gen Left_appr_se = .
gen Right_diss = .
gen Right_diss_se = .
gen Right_appr = .
gen Right_appr_se = .
gen Left_diss_error = .  
gen Left_appr_error = .  
gen Right_diss_error = . 
gen Right_appr_error = .

*Weak
forval k = 1/22 {
mean Q2_`k'_diss if ideology_cat == 1 & ID_strength<=0.5
matrix T = r(table)
replace Left_diss = T[1,1] in `k'
replace Left_diss_se = T[2,1] in `k'

mean Q2_`k'_appr if ideology_cat == 1 & ID_strength<=0.5
matrix T = r(table)
replace Left_appr = T[1,1] in `k'
replace Left_appr_se = T[2,1] in `k'


mean Q2_`k'_diss if ideology_cat == 2  & ID_strength<=0.5
matrix T = r(table)
replace Right_diss = T[1,1] in `k'
replace Right_diss_se = T[2,1] in `k'

mean Q2_`k'_appr if ideology_cat == 2 & ID_strength<=0.5
matrix T = r(table)
replace Right_appr = T[1,1] in `k'
replace Right_appr_se = T[2,1] in `k'
}

replace Left_diss_error = 1.96*Left_diss_se

replace Left_appr_error = 1.96*Left_appr_se

replace Right_diss_error = 1.96*Right_diss_se

replace Right_appr_error = 1.96*Right_appr_se

egen right_W = rowmax(Right_appr Right_diss)
replace right_W = Right_appr in 9 
replace right_W = Right_diss in 17 
replace right_W = -right_W if Right_diss>Right_appr
replace right_W = -right_W in 9 
replace right_W = -right_W in 17 
gen right_W_error = Right_diss_error if Right_diss>Left_appr
replace right_W_error = Right_appr_error if Right_diss<Left_appr
replace right_W_error = Right_appr_error in 9 
replace right_W_error = Right_diss_error in 17 


egen left_W = rowmax(Left_appr Left_diss)
replace left_W = -left_W if Left_diss>Left_appr
gen left_W_error = Left_diss_error if Left_diss>Left_appr
replace left_W_error = Left_appr_error if Left_diss<Left_appr

*Strong
forval k = 1/22 {
mean Q2_`k'_diss if ideology_cat == 1  & ID_strength>0.5
matrix T = r(table)
replace Left_diss = T[1,1] in `k'
replace Left_diss_se = T[2,1] in `k'

mean Q2_`k'_appr if ideology_cat == 1  & ID_strength>0.5
matrix T = r(table)
replace Left_appr = T[1,1] in `k'
replace Left_appr_se = T[2,1] in `k'


mean Q2_`k'_diss if ideology_cat == 2  & ID_strength>0.5
matrix T = r(table)
replace Right_diss = T[1,1] in `k'
replace Right_diss_se = T[2,1] in `k'

mean Q2_`k'_appr if ideology_cat == 2  & ID_strength>0.5
matrix T = r(table)
replace Right_appr = T[1,1] in `k'
replace Right_appr_se = T[2,1] in `k'
}

replace Left_diss_error = 1.96*Left_diss_se

replace Left_appr_error = 1.96*Left_appr_se

replace Right_diss_error = 1.96*Right_diss_se

replace Right_appr_error = 1.96*Right_appr_se

egen right_S = rowmax(Right_appr Right_diss)
replace right_S = -right_S if Right_diss>Right_appr
gen right_S_error = Right_diss_error if Right_diss>Left_appr
replace right_S_error = Right_appr_error if Right_diss<Left_appr

egen left_S = rowmax(Left_appr Left_diss)
replace left_S = -left_S if Left_diss>Left_appr
gen left_S_error = Left_diss_error if Left_diss>Left_appr
replace left_S_error = Left_appr_error if Left_diss<Left_appr

*Calculate IQR
gen Left_median = .
gen Left_P25 = .
gen Left_P75 = .
gen Left_IQR = .

gen Right_median = .
gen Right_P25 = .
gen Right_P75 = .
gen Right_IQR = .

forval k = 1/22 {
sum Q2_`k' if ideology_cat == 1, d
replace Left_median = r(p50) in `k'
replace Left_P25 = r(p25) in `k'
replace Left_P75 = r(p75) in `k'
}
replace Left_IQR = Left_P75 - Left_P25


forval k = 1/22 {
sum Q2_`k' if ideology_cat == 2, d
replace Right_median = r(p50) in `k'
replace Right_P25 = r(p25) in `k'
replace Right_P75 = r(p75) in `k'
}
replace Right_IQR = Right_P75 - Right_P25

*Compare weak to strong
gen Left_diss_pvalue = .
gen Left_appr_pvalue = .
gen Right_diss_pvalue = .
gen Right_appr_pvalue = .
recode ID_strength (0/0.5 = 1 "weak") (0.51/1 = 2 "strong"), into(weak_vs_strong)

forval k = 1/22 {
ttest Q2_`k'_diss if ideology_cat == 1, by(weak_vs_strong) 
replace Left_diss_pvalue = r(p) in `k'

ttest Q2_`k'_appr if ideology_cat == 1, by(weak_vs_strong) 
replace Left_appr_pvalue = r(p) in `k'

ttest Q2_`k'_diss if ideology_cat == 2, by(weak_vs_strong) 
replace Right_diss_pvalue = r(p) in `k'

ttest Q2_`k'_appr if ideology_cat == 2, by(weak_vs_strong) 
replace Right_appr_pvalue = r(p) in `k'
}

gen left_pvalue = Left_diss_pvalue if Left_diss>Left_appr
replace left_pvalue = Left_appr_pvalue if Left_diss<Left_appr

gen right_pvalue = Right_diss_pvalue if Right_diss>Left_appr
replace right_pvalue = Right_appr_pvalue if Right_diss<Left_appr

*******************************
*  Results files for Figure 1 *
*******************************

save "Study 1 US Appendix.dta", replace

keep issue right_W right_W_error left_W left_W_error right_S right_S_error left_S left_S_error Left_IQR Right_IQR left_pvalue right_pvalue

save "Study 1 US results.dta", replace

drop left_W left_W_error left_S left_S_error Left_IQR left_pvalue

sort right_S

save "Study 1 US results right.dta", replace

use "Study 1 US results.dta"

drop  right_W right_W_error right_S right_S_error Right_IQR right_pvalue

sort left_S

save "Study 1 US results left.dta", replace


**********************
*     Appendix 1     *
**********************
use "ANES subset 1.dta", clear
recode C5_K1 (1/3 = 1 "Liberal") (4 = 3 "Centre") (5/7 = 2 "Conservative") (-7/-6 = 4 "DK"), into(lib_cons_2013)
recode libcpre_self (1/3 = 1 "Liberal") (4 = 3 "Centre") (5/7 = 2 "Conservative") (-9/-2 = 4 "DK"), into(lib_cons_2012)

tab lib_cons_2012 lib_cons_2013 if lib_cons_2012>0 & lib_cons_2013>0, row nofreq
tab lib_cons_2012 lib_cons_2013 if lib_cons_2012>0 & lib_cons_2013>0


**********************
*     Appendix 6     *
**********************
*Distribution of Normatively Correct Responses 
use "Study 1 US Appendix.dta", clear
gen ID_strength_bin = 0 
replace ID_strength_bin = 1 if ID_strength>0.5

gen Con_correct = (Q2_1_appr + Q2_3_appr  + Q2_4_diss + Q2_5_appr + Q2_6_diss + Q2_7_diss + Q2_8_appr + Q2_9_appr + Q2_10_diss + Q2_11_diss + Q2_12_diss + Q2_13_appr + Q2_14_diss + Q2_15_appr + Q2_18_diss + Q2_20_appr + Q2_22_appr)/17 if ideology_cat3 == 2  
gen Lib_correct = (Q2_1_diss + Q2_2_appr + Q2_3_diss + Q2_4_appr + Q2_5_diss + Q2_6_appr + Q2_7_appr + Q2_8_diss + Q2_9_diss + Q2_10_appr + Q2_11_diss + Q2_13_appr + Q2_14_appr + Q2_15_appr + Q2_16_appr + Q2_17_appr + Q2_18_appr + Q2_19_appr + Q2_20_appr + Q2_21_appr + Q2_22_diss)/21  if ideology_cat3 == 1 


twoway (histogram Con_correct if ID_strength_bin == 0, lcolor(none) percent  bin(10)) (histogram Con_correct if ID_strength_bin == 1, fcolor(none) lcolor(black) percent  bin(10)), ytitle(Percent of Conservative Respondents) xtitle(Proportion Correct) legend(order(1 "Weak SID" 2 "Strong SID" )) 
graph save Graph "Figure A2b.gph"
twoway (histogram Lib_correct if ID_strength_bin == 0, lcolor(none) percent  bin(10)) (histogram Lib_correct if ID_strength_bin == 1, fcolor(none) lcolor(black) percent  bin(10)), ytitle(Percent of Liberal Respondents) xtitle(Proportion Correct) legend(order(1 "Weak SID" 2 "Strong SID" )) 
graph save Graph "Figure A2a.gph"

*Factor analysis 
factor Q2_1-Q2_22 if ideology_cat3 == 2
factor Q2_1-Q2_22 if ideology_cat3 == 2, factor(4)  
rotate
